# config/database_config.py
import os
import sqlite3
from datetime import datetime
from typing import Dict, Any
import logging

class DatabaseConfig:
    """数据库配置类"""

    # 默认配置
    DEFAULT_CONFIG = {
        'db_path': 'data/stock_data.db',  # 数据库文件路径
        'backup_path': 'data/backups/',   # 备份路径
        'max_db_size_mb': 1000,           # 最大数据库大小(MB)
        'auto_backup': True,              # 是否自动备份
        'backup_interval_days': 7,        # 备份间隔天数
    }

    def __init__(self, config: Dict[str, Any] = None):
        self.config = {**self.DEFAULT_CONFIG, **(config or {})}
        self._ensure_directories()
        self.logger = logging.getLogger(__name__)

    def _ensure_directories(self):
        """确保所需目录存在"""
        os.makedirs(os.path.dirname(self.get_db_path()), exist_ok=True)
        os.makedirs(self.get_backup_path(), exist_ok=True)

    def get_db_path(self) -> str:
        """获取数据库文件路径"""
        return self.config['db_path']

    def get_backup_path(self) -> str:
        """获取备份路径"""
        return self.config['backup_path']

    def get_connection_string(self) -> str:
        """获取数据库连接字符串"""
        return f"sqlite:///{self.get_db_path()}"

    def should_auto_backup(self) -> bool:
        """是否自动备份"""
        return self.config['auto_backup']